﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Web;

/// <summary>
/// Summary description for CookieMonstar
/// </summary>
public class CookieMonstar
{
    public CookieMonstar()
    {

    }

    //
    // TODO: Add constructor logic here
    //
    public static void AddPathToCookies(string path)
    {

        Debug.Print("\nAddPathToCookies\n======================================");

        Char[] DelimiterArray = new Char[1] { '\n' };
        string[] saPath = path.Split(DelimiterArray);
        Debug.Print("path=" + path);

        if (saPath.Length > 1)
        {
            //save page URL to Cookie
            HttpCookie cookieHistory = HttpContext.Current.Request.Cookies.Get("ACC3_0.History");
            if (cookieHistory == null)
            {
                Debug.Print("cookieHistory == null");
                cookieHistory = new HttpCookie("ACC3_0.History");
                //cookieHistory.Expires.AddMonths(1);
                Debug.Print("cookieHistory CREATED");

            }
            else
            {
                Debug.Print("cookieHistory.Values.Count=" + cookieHistory.Values.Count);
                for (int i = cookieHistory.Values.Count - 1; i >= 0; i--)
                {
                    ListItem mnuItem = new ListItem(cookieHistory.Values.AllKeys[i], cookieHistory.Values[i]);
                    Debug.Print(string.Format("cookieHistory.Values[{0}] :=: {1}", i, mnuItem.Text + "=" + mnuItem.Value));
                }
            }

            string sPath = saPath[0];
            for (int i = 2; i < saPath.Length; i++)
            {
                sPath += "-" + saPath[i];
            }
            Debug.Print("sPath=" + sPath);

            if (!cookieHistory.Values.AllKeys.Contains(sPath))
            {
                cookieHistory.Expires.AddMonths(1);
                cookieHistory.Values.Add(sPath, HttpContext.Current.Request.Url.AbsoluteUri);
                Debug.Print("cookieHistory.Values[cookieHistory.Values.Length-1]=" + cookieHistory.Values[cookieHistory.Values.Count - 1]);

                HttpContext.Current.Response.Cookies.Add(cookieHistory);

            }

            else
            {
                Debug.Print("cookieHistory.Values.AllKeys.Contains(sPath)");
            }
        }
    }

    public static void AddUserHistoryToList(ref BulletedList blstHistory)
    {

        Debug.Print("\nAddUserHistoryToList\n======================================");
        Debug.Print("blstHistory.Count=" + blstHistory.Items.Count);
        //get the user's History into menuitems from collection in cookie:      


        HttpCookie cookieHistory = HttpContext.Current.Request.Cookies["ACC3_0.History"];

        if (cookieHistory != null && cookieHistory.HasKeys)
        {
            Debug.Print("cookieHistory.Values.Count=" + cookieHistory.Values.Count);
            for (int i = cookieHistory.Values.Count - 1; i >= 0; i--)
            {
                ListItem mnuItem = new ListItem(cookieHistory.Values.AllKeys[i], cookieHistory.Values[i]);
                Debug.Print(string.Format("cookieHistory.Values[{0}] :=: {1}", i, mnuItem.Text + "=" + mnuItem.Value));

                Int32 nInt;
                if (blstHistory.Items.FindByText(mnuItem.Text) == null
                 && !Int32.TryParse(mnuItem.Text, out nInt))
                {
                    blstHistory.Items.Add(mnuItem);
                    Debug.Print("Added to blstHistory.Count=" + blstHistory.Items.Count);
                }
            }

        }
        else
        {
            Debug.Print("cookieHistory == null");
        }
    }


    public static void setCookie(string psCookie, string psValue, DateTime pdtExp)
    {
        if (HttpContext.Current != null)
        {
            if (HttpContext.Current.Request.Cookies[psCookie] == null)
                HttpContext.Current.Response.Cookies.Add(new HttpCookie(psCookie, psValue));
            else
                HttpContext.Current.Response.Cookies[psCookie].Value = psValue;
            HttpContext.Current.Request.Cookies[psCookie].Value = psValue;
            HttpContext.Current.Response.Cookies[psCookie].Expires = pdtExp;
        }
    }


    public static string getCookie(string psCookieName)
    {
        return (HttpContext.Current != null) ? null : HttpContext.Current.Request.Cookies[psCookieName] == null ? "" :
                      HttpContext.Current.Request.Cookies[psCookieName].Value;

    }

}